package com.yc.video.support.utils.video;


import com.yc.video.support.utils.DateUtil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * <h5>功能:视频文件处理工具类</h5>
 */

public class VideoUtils {

	private static String ffmpegEXE = "D:\\ycworkspace\\utils-soft\\ffmpeg-win64\\bin\\ffmpeg.exe";//ffmpeg程序路径

	private static final Logger log = LoggerFactory.getLogger(VideoUtils.class);

	/**
	 * 开启线程处理Ffmpeg处理流
	 *
	 * @param process
	 */
	private static void dealStream(Process process) {
		if (process == null) {
			return;
		}
		// 处理InputStream的线程
		new Thread() {
			@Override
			public void run() {
				BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
				String line = null;
				try {
					while ((line = in.readLine()) != null) {
						log.warn("output: " + line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		// 处理ErrorStream的线程
		new Thread() {
			@Override
			public void run() {
				BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
				String line = null;
				try {
					while ((line = err.readLine()) != null) {
						log.warn("output: " + line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						err.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	public static void main(String[] args) {

//		String fileName = "C:\\Users\\Administrator\\Desktop\\防沉使用.mp4";
//		String time = DateUtil.getTime();
//		String outName = "C:\\Users\\Administrator\\Desktop\\防沉使用.jpg";
//		String size = "1920x1080";
//		VideoUtils.covPic( fileName,  time,  outName,  size);
		VideoUtils.test01();

	}

	public static void test01() {

		try {
//			FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault
//					("C:\\Users\\Administrator\\Desktop\\截图\\点点极光\\CSGO项目图集\\csgo淘宝图\\总说明.mp4");
			FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault
					("C:\\Users\\Administrator\\Desktop\\VID_20141121_160752.3gp");
			ff.start();
			int ffLength = ff.getLengthInFrames();
			Frame f;
			String pngPath = "";
			int i = 0;
			while (i < ffLength) {
				f = ff.grabImage();
				//截取第6帧
				if ((i > 5) && (f.image != null)) {
					//截图并放入指定位置
					Java2DFrameConverter converter = new Java2DFrameConverter();
					BufferedImage bi = converter.getBufferedImage(f);
					pngPath = "C:\\Users\\Administrator\\Desktop\\" + DateUtil.getAllTime() + Math.random() + ".png";
					File output = new File(pngPath);
					ImageIO.write(bi, "png", output);
					if (i > 10) break;
				}
				i++;
			}
			ff.stop();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


}